package com.cloudptt.wp.ui.call;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.ISuperCallOperator;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallListener;
import com.cloudptt.api.product.func.call.group.fsm.video.GroupCallMemberInfo;
import com.cloudptt.api.product.func.call.single.LaunchCallInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.mcptt.db.model.AddrInfo;

import java.util.List;


public class CallActivity extends Activity implements CallListener {

	public static String THIS_FILE = "MainActivity";
	public TextView nameTextView = null;
	public TextView numTextView = null;
	public TextView dscpTextView = null;
	public Button leftBtn = null;
	public Button rightBtn = null;
	public View flagView = null;

	private WakeLock wakeLock;
	private Product product = null;
	private ISuperCallOperator op = null;

	private LaunchCallInfo callInfo = null;
	private AddrInfo addrInfo = null;
	private String curCallKey = null;
	private int callState = CallConst.CallState_InitState;
//	private int callDir = CallAttr.CallDir_MO;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i(THIS_FILE, "On onCreate");
		setContentView(R.layout.activity_call);

		initView();
		ScreenManager.getScreenManager().pushActivity(this);
	}

	private void initView() {
		Log.i(THIS_FILE, "mainactiviy initView");
		setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);

		this.callInfo = (LaunchCallInfo) getIntent().getSerializableExtra(SipManager.EXTRA_PRIVACY_CALL_INFO);
		this.curCallKey = callInfo.getCallIdStr();
		this.callState = callInfo.getCallState();
		this.addrInfo = (AddrInfo) getIntent().getSerializableExtra(SipManager.EXTRA_PRIVACY_CALL_USER);

		Log.e(THIS_FILE, "onCreate callInfo = " + callInfo + " curCallKey = " + curCallKey + " userInfo = " + addrInfo);

		nameTextView = (TextView) findViewById(R.id.nameTextView);
		numTextView = (TextView) findViewById(R.id.numTextView);
		dscpTextView = (TextView) findViewById(R.id.dscpTextView);
		leftBtn = (Button) findViewById(R.id.leftBtn);
		rightBtn = (Button) findViewById(R.id.rightBtn);
		flagView = (View) findViewById(R.id.flagView);
	}

	@Override
	protected void onResume() {
		Log.i(THIS_FILE, "On Resume");
		super.onResume();
		initDate();
		setListener();
	}

	private void initDate()
	{
		if(addrInfo != null)
		{
			String name = addrInfo.getName();
			nameTextView.setText(name);
			String num = addrInfo.getAddrId();
			numTextView.setText(num);
		}
		if(callInfo.isMoCall())
		{
			flagView.setBackgroundResource(R.drawable.flag_mo);
			dscpTextView.setText(R.string.str_mo);
		}
		else
		{
			flagView.setBackgroundResource(R.drawable.flag_mt);
			dscpTextView.setText(R.string.str_mt);
		}
		updateCallState(curCallKey,callState,false);
	}
	private void setListener() {
		if (product == null) {
			SysApplication app = ((SysApplication) getApplicationContext());
			product = app.getProduct();
		}
		if (product == null) {
			Log.e(THIS_FILE, "onCreate product == null");
		}
		if (product != null) {
			product.setSingleCallListener(this);
		}
	}
//	@Override
//	public void incomingCall(String callKey,AddrInfo incomingInfo)
//	{}
	@Override
	public void updateCallState(String callKey, int callState, boolean hasVideo) {
		Log.e(THIS_FILE, "updateCallState callKey = " + callKey + " callState = " + callState + " hasVideo = " + hasVideo);
		this.callState = callState;
		if (callState == CallConst.CallState_ConnectingState) {
			if (callInfo != null) {
				if (callInfo.isMoCall()) {
					leftBtn.setText(R.string.str_cancel);
					rightBtn.setText(R.string.str_speaker);
				} else {
					leftBtn.setText(R.string.str_accept);
					rightBtn.setText(R.string.str_reject);
				}
			}

		} else if (callState == CallConst.CallState_Activite) {
			Log.e(THIS_FILE, "updateCallState callInfo = " + callInfo);
			leftBtn.setText(R.string.str_hangup);
			rightBtn.setText(R.string.str_speaker);
			dscpTextView.setText("00:00");
			numTextView.setVisibility(View.INVISIBLE);
		} else if (callState == CallConst.CallState_InitState) {
			// voiceSettingBtn.setVisibility(View.GONE);
			this.finish();
		}
	}

	@Override
	public void notifyErrStr(String callKey, String errStr) {

	}

	@Override
	public void updateCallDuration(String callKey, String secStr) {
//		if (numTextView != null) {
//			numTextView.setVisibility(View.INVISIBLE);
//		}
		if(dscpTextView != null)
		{
			//dscpTextView.setVisibility(View.VISIBLE);
			dscpTextView.setText(secStr);
		}
	}

	@Override
	public void updateMicMute(String callKey, boolean isMicMute) {
		if(isMicMute)
		{
			rightBtn.setText(R.string.str_receiver);
		}
		else
		{
			rightBtn.setText(R.string.str_speaker);
		}
	}

	@Override
	public void updateMediaStatus(String callKey, int mediaStatus) {

	}
	@Override
	public void updateVideoEvent(String callKey)
	{}
	@Override
	public void updateGroupCallMemberInfoList(List<GroupCallMemberInfo> groupCallMemberInfoList)
	{}
	@Override
	public void finish() {
		Log.i(THIS_FILE, " finish()");
		super.finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, " onDestroy()");
		if (wakeLock!= null) 
		{          
			try 
			{
				wakeLock.release();
            } catch (Throwable th) {
                // ignoring this exception, probably wakeLock was already released
            }
			 wakeLock = null; 
		}
		super.onDestroy();
	}



	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d(THIS_FILE, "onKeyDown keyCode "+ keyCode);

		if (keyCode == KeyEvent.KEYCODE_SOFT_LEFT)
		{
			chickLeftBtn();
			return false;
		}
		else if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			chickRightBtn();
			return false;
		}
		else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ){
			AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			am.adjustStreamVolume( AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_LOWER,
					AudioManager.FLAG_SHOW_UI  );
            return true;
        }else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        	AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        	am.adjustStreamVolume( AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_RAISE,
        			AudioManager.FLAG_SHOW_UI );
        	return true;
        }
		return super.onKeyDown(keyCode, event);
	}

	private void chickLeftBtn()
	{
		if (callState == CallConst.CallState_ConnectingState) {
			if (callInfo != null) {
				if (callInfo.isMoCall()) {
					doAction(CallConst.BtnAction_Hangup);
				} else {
					doAction(CallConst.BtnAction_Answer);
				}
			}

		} else if (callState == CallConst.CallState_Activite) {
			Log.e(THIS_FILE, "updateCallState callInfo = " + callInfo);
			doAction(CallConst.BtnAction_Hangup);
		}
	}

	private void chickRightBtn()
	{
		if (callState == CallConst.CallState_ConnectingState) {
			if (callInfo != null) {
				if (callInfo.isMoCall()) {
					doAction(CallConst.BtnAction_MicMute);
				} else {
					doAction(CallConst.BtnAction_Hangup);
				}
			}

		} else if (callState == CallConst.CallState_Activite) {
			Log.e(THIS_FILE, "updateCallState callInfo = " + callInfo);
			doAction(CallConst.BtnAction_MicMute);

		}
	}

	private void doAction(int action) {
		if (product == null) {
			SysApplication app = ((SysApplication) getApplicationContext());
			product = app.getProduct();
		}
		if (product != null) {
			ISuperCallOperator op = product.getSuperCallOperator();
			op.doUserAction(curCallKey, action,"");
		}
	}
}
